<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>订单页面</title>
    <!--引入资源：vue要在element之前引入-->
    <script src="https://cdn.bootcdn.net/ajax/libs/vue/2.6.13/vue.js"></script>
    <script src="https://cdn.bootcdn.net/ajax/libs/element-ui/2.15.14/index.js"></script>
    <link href="https://cdn.bootcdn.net/ajax/libs/element-ui/2.15.14/theme-chalk/index.css" rel="stylesheet">
    <script src="https://cdn.bootcdn.net/ajax/libs/axios/1.5.0/axios.js"></script>
    <script src="/js/axios.helper.js"></script>
    <style>
        .el-row {
            margin-bottom: 20px;
        &:last-child {
             margin-bottom: 0;
         }
        }
        .el-col {
            border-radius: 4px;
        }
        .bg-purple-dark {
            background: #99a9bf;
        }
        .bg-purple-light {
            background: #e5e9f2;
        }
        .grid-content {
            border-radius: 4px;
            min-height: 36px;
            display: flex;
            justify-content: center; /* 水平居中 */
            align-items: center; /* 垂直居中 */
        }
    </style>
</head>
<body>
    <div id="app">
        <el-container>
            <el-header>
                <!--第一行：头部-->
                <el-row>
                    <el-col :span="24"><div class="grid-content bg-purple-light">蜗牛学苑</div></el-col>
                </el-row>
            </el-header>
            <el-main>
                <!--第二行：搜索-->
                <el-row>
                    <el-col :span="12">
                        <!--第二行：左边搜索-->
                        <el-form :inline="true" :model="condition" class="demo-form-inline">
                            <el-form-item label="请输入">
                                <el-input v-model="condition.keyword" placeholder="书名/作者/类型"></el-input>
                            </el-form-item>
                            <el-form-item>
                                <el-button type="primary" @click="queryBook">查询</el-button>
                            </el-form-item>
                        </el-form>
                    </el-col>
                    <el-col :span="12">
                        <!--第二行：右边 个人中心 我的购物车 我的订单-->
                        <div style="text-align: right">

                            <!--未登录显示-->
                            <span v-if="nickName == ''">
                                <el-link type="info" :href="'/login.html'">登录</el-link>
                                <el-link type="info" :href="'/register.html'">注册</el-link>
                            </span>
                            <span v-else>
                                欢迎你,{{nickName}}
                                <el-link type="info" @click="logout">退出系统</el-link>
                            </span>

                            <el-link type="info" :href="'/cart.html'">我的购物车</el-link>
                            <el-link type="info">我的订单</el-link>
                        </div>
                    </el-col>
                </el-row>

                <!--第三行：面包屑； 类别导航-->
                <el-row>
                    <el-col :span="24">
                        <el-breadcrumb separator="/">
                            <el-breadcrumb-item v-for="type in typeList">
                                <el-link :href="`/index/showBookList?typeId=${type.id}`" type="info" style="color: gray; font-weight: normal">{{type.name}}</el-link>
                            </el-breadcrumb-item>
                        </el-breadcrumb>
                    </el-col>
                </el-row>

                <!--选项卡Tabs-->
                <el-row>
                    <el-col :span="24">
                        <el-tabs v-model="activeName" type="card" @tab-click="handleClick">
                            <el-tab-pane label="全部订单" name="1">
                                <!--此时显示全部订单-->

                            </el-tab-pane>
                            <el-tab-pane label="待付款" name="2">

                                <div v-for="(o,index) in waitPayOrders" :key="index">
                                    <!--此处显示所有待付款的订单-->
                                    <el-descriptions title="订单信息">
                                        <el-descriptions-item label="下单时间" >{{o.createtime}}</el-descriptions-item>
                                        <el-descriptions-item label="订单编号">{{o.orderNum}}</el-descriptions-item>
                                        <el-descriptions-item label="订单总金额（元）">{{o.totalprice}}</el-descriptions-item>
                                        <el-descriptions-item label="收货人">{{o.receiver}}</el-descriptions-item>
                                        <el-descriptions-item label="订单状态">{{o.orderStatus}}</el-descriptions-item>
                                    </el-descriptions>

                                    <el-table :data="o.orderItem" style="width: 100%">
                                        <el-table-column prop="bookName" label="书籍名称" width="180"></el-table-column>
                                        <el-table-column label="书籍图片" width="180">
                                            <template slot-scope="scope">
                                                <el-image style="width: 100px; height: 100px" :src="scope.row.bookImg"></el-image>
                                            </template>
                                        </el-table-column>
                                        <el-table-column prop="bcount" label="数量"> </el-table-column>
                                        <el-table-column prop="sumprice" label="小计"></el-table-column>
                                    </el-table>

                                    <p>
                                        <el-button type="primary" @click="payOrder(o.id)">去支付</el-button>
                                        <el-button type="info">取消购买</el-button>
                                    </p>

                                </div>
                            </el-tab-pane>

                            <el-tab-pane label="待发货" name="3">
                                <!--此处显示所有待发货的订单-->
                            </el-tab-pane>
                            <el-tab-pane label="待收货" name="4"></el-tab-pane>
                            <el-tab-pane label="待评价" name="5"></el-tab-pane>
                            <el-tab-pane label="退货中" name="6"></el-tab-pane>
                            <el-tab-pane label="退款中" name="7"></el-tab-pane>
                            <el-tab-pane label="已取消" name="8"></el-tab-pane>
                        </el-tabs>
                    </el-col>
                </el-row>

            </el-main>
        </el-container>
    </div>
</body>
<script>
    new Vue({
        el: "#app",
        data(){
            return {
                // 搜索条件
                condition:{
                    keyword: ''
                },
                typeList:[],
                nickName: '',
                activeName: '2',
                waitPayOrders:[],
                uni_pay_code: ''
            }
        },
        methods: {
            // 图书搜索
            queryBook(){

            },
            // 查询图书分类列表
            queryBookType(){
                axios.get('/booktype').then(resp=>{
                     this.typeList = resp.data.data;
                })
            },
            // 查询登录用户昵称
            getLoginName() {
                // axios请求，会通过拦截器携带token到后台
                axios.get('/user/getLoginName').then(resp=>{
                    if (resp.data.code == 200) {
                        this.nickName = resp.data.data;
                    } else {
                        this.nickName = ''
                    }
                })
            },
            logout() {
                this.$confirm('确定退出?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    localStorage.removeItem("token");
                    location.href = '/index.html'
                })

            },
            handleClick(tab, event) {
                console.log(tab, event);
            },
            // 查询待支付订单
            queryWaitPayOrders(){
                // 查询待支付的订单，后台返回OrderVo
                axios.get('/order/waitPay').then(res=>{
                    console.log("查询待支付订单:",res);
                    this.waitPayOrders = res.data.data;
                });
            },
            // 去支付
            payOrder(orderId){
                console.log("支付订单id：",orderId);
                const id = orderId.toString(); // 最好转换一下，解决雪花id传参可能会导致的精度丢失问题
                axios.post(`/order/pay/${id}`,{},{
                    headers: {
                        "uniqueCode": this.uni_pay_code
                    }
                }).then(resp=>{
                    if (resp.data.code == 200) {
                        this.$message.success("购买成功!");
                        setTimeout(()=>{
                            location.href = '/index.html';
                        },2000)
                    } else {
                        this.$message.success(resp.data.message);
                    }
                })
            }
        },
        created(){
            // 查询图书分类列表
            this.queryBookType();
            // 查询登录用户昵称
            this.getLoginName();
            // 查询待支付订单
            this.queryWaitPayOrders();
            this.uni_pay_code = "uni_pay_code_" + Date.now();
        }
    })
</script>
</html>